<?php
class Synthesis_Workbench_Helper_Data extends Mage_Core_Helper_Abstract
{
	public function FileSize($file, $setup = null)
	{
		$FZ = ($file && @is_file($file)) ? filesize($file) : NULL;
		$FS = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
	   
		if(!$setup && $setup !== 0)
		{
			return number_format($FZ/pow(1024, $i=floor(log($FZ, 1024))), ($i >= 1) ? 2 : 0) . ' ' . $FS[$i];
		} elseif ($setup == 'INT') return number_format($FZ);
		else return number_format($FZ/pow(1024, $setup), ($setup >= 1) ? 2 : 0 ). ' ' . $FS[$setup];
	}
	
	public function getExecutionTime($url)
	{
		if(!isset($url))
			die('Url was not specified!');

		$start = $this->microtimeFloat();

		file_get_contents($url);
		
		$end = $this->microtimeFloat();

		echo round($end - $start, 3) . ' s';
	}
	
	// Script execution time.
	public function microtimeFloat()
	{
		list ($msec, $sec) = explode(' ', microtime());
		$microtime = (float)$msec + (float)$sec;
		return $microtime;
	}
	
	public function getCachePath()
	{
		return Mage::getBaseDir('var') . DS . 'cache';
	}
	
	public function getDesignPath()
	{
		return Mage::getBaseDir('media') . DS . 'design';
	}
	
	public function getDesignUrl($file)
	{
		return Mage::getUrl('media/design') . $file;
	}
	
	public function getReportsPath()
	{
		return Mage::getBaseDir('var') . DS . 'report';
	}
	
	public function designFiles($return = false)
	{
		$dirname = self::getDesignPath();
		$ext = array('jpg', 'png', 'jpeg', 'gif');
		$search = array('jpg', 'png', 'jpeg', 'gif','_', '-', '.');
		$replace = array('', '', '', '', ' ', ' ', ' ');
		$files = array();
		
		if($handle = opendir($dirname)) {
		   while(false !== ($file = readdir($handle)))
			   for($i=0;$i<sizeof($ext);$i++)
				   if(strstr($file, ".".$ext[$i])){
						$files[] = array(
										'file-name' => $file,
										'title' => ucfirst(str_replace($search, $replace, $file))
										);
					}

			closedir($handle);
		}

		Mage::register('workbench_design', $files);
		
		if(count($files) && $return)
			return $this->designFile = self::getDesignUrl($files[0]['file-name']);		
		
	}
	
	public function deleteDirectory($dir)
	{

		if (!file_exists($dir)) return true;
		if (!is_dir($dir) || is_link($dir)) return unlink($dir);
		
		foreach (scandir($dir) as $item) {
			if ($item == '.' || $item == '..') continue;
			if (!$this->deleteDirectory($dir . DS . $item)) {
				chmod($dir . DS . $item, 0777);
				if (!$this->deleteDirectory($dir . DS . $item)) return false;
			};
		}
		
		return rmdir($dir); 
	}
	
	public function getReportCount()
	{
		$dir = $this->getReportsPath();
		$count = 0;
		
		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if($file != '.' && $file != '..')
						$count++;
				}
				closedir($dh);
			}
		}
		return $count;
	}
	
    protected function getPathConfig($path, $full = true)
    {
        $configDataCollection = Mage::getModel('core/config_data')
            ->getCollection()
			->addFieldToFilter('scope_id', Mage::app()->getStore()->getStoreId())
			->addFieldToFilter('scope', 'stores')
            ->addFieldToFilter('path', $path);
			
        return $configDataCollection->getFirstItem()->getValue();
    }
	
	public function toggleConfig($section, $group, $field)
	{
		
		$groups = array($group => array( 'fields' => array( $field => array( 'value' => (self::getPathConfig($section . '/' . $group . '/' . $field)) ? 0 : 1 ))));
		Mage::getModel('adminhtml/config_data')
                ->setSection($section)
                ->setWebsite(Mage::app()->getStore()->getWebsite()->getCode())
                ->setStore(Mage::app()->getStore()->getCode())
                ->setGroups($groups)
                ->save();	
	}
	

	public function getLogsEnabled()
	{
		if(Mage::getStoreConfig('dev/log/active'))
			return true;
		else
			return false;
	}
	

	public function getHintsEnabled()
	{
		if(Mage::getStoreConfig('dev/debug/template_hints'))
			return true;
		else
			return false;
	}
	

	public function getTranslationEnabled()
	{
		if(Mage::getStoreConfig('dev/translate_inline/active'))
			return true;
		else
			return false;
	}
	
}